package net.starwifi.duobao.hfg.payment;

import cn.com.hongpay.client.utils.Md5Encoder;
import cn.com.hongpay.client.utils.SignatureTools;
import com.alibaba.fastjson.JSONObject;
import net.starwifi.duobao.hfg.APIRequest;
import net.starwifi.duobao.utils.DesHelper;
import org.jsoup.helper.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hui on 2016/4/10.
 */
public class PaymentRequest extends APIRequest {

    private String login_name;

    private String partner_id;

    private String partner_verify_code;

    private String request_time;

    private String signed_msg;

    private List <PayTradeDetail> trade_details=new ArrayList<PayTradeDetail>();

    private TransSum trans_sum;

    public PaymentRequest(String login_name, String partner_id, String partner_verify_code, String request_time, PayTradeDetail trade_detail, TransSum trans_sum) {
        try {
            this.login_name=login_name;
            if (!StringUtil.isBlank(login_name)){
            this.login_name = DesHelper.toHexString(DesHelper.encrypt(login_name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.partner_id = partner_id;
        this.partner_verify_code = Md5Encoder.encode(partner_verify_code+request_time);;
        this.request_time = request_time;
        this.trade_details.add(trade_detail);
        this.trans_sum = trans_sum;
    }

    @Override
    public String makeSign(String pfxFile, String pfxPwd, String privateKeyPwd) {
        StringBuffer sb=new StringBuffer();
        PayTradeDetail tradeDetail=trade_details.get(0);
        if (!StringUtil.isBlank(login_name)){
            sb.append("login_name:"+login_name+";");
        }
        sb.append("partner_id:"+partner_id);
        sb.append(";partner_verify_code:"+partner_verify_code);
        sb.append(";request_time:"+request_time);
        sb.append(";trade_remark:"+trans_sum.getTrade_remark());
        sb.append(";partner_trade_no:"+tradeDetail.getPartner_trade_no());
        sb.append(";payee_partner_acct_id:"+tradeDetail.getPayee_partner_acct_id());
        sb.append(";goods_name:"+tradeDetail.getGoods_name());
        sb.append(";pay_amount:"+tradeDetail.getPay_amount());
        sb.append(";goods_type:"+tradeDetail.getGoods_type());
        sb.append(";detail_remark:"+tradeDetail.getDetail_remark()+";");
        String signStr=sb.toString();
        System.out.println("makeSign:"+signStr);
        String sign= SignatureTools.sign(signStr,"UTF-8",pfxFile, pfxPwd, privateKeyPwd);
        System.out.println("加密makeSign:"+sign);
        return sign;
    }

    @Override
    public String encode(String pfxFile, String pfxPwd, String privateKeyPwd){
        String sign=makeSign(pfxFile,pfxPwd,privateKeyPwd);
        PayHead head = new PayHead(login_name,partner_id,partner_verify_code,request_time,sign);
        String headJson= JSONObject.toJSONString(head);
        String detailJson=JSONObject.toJSONString(trade_details);
        String sumJson=JSONObject.toJSONString(trans_sum);
        String json="{ \"body\":{\"trade_details\":"+detailJson+",\"trans_sum\":"+sumJson+"},\"head\":"+headJson+"}";
        return json;
    };



    public String getLogin_name() {
        return login_name;
    }

    public void setLogin_name(String login_name) {
        this.login_name = login_name;
    }

    public String getPartner_id() {
        return partner_id;
    }

    public void setPartner_id(String partner_id) {
        this.partner_id = partner_id;
    }

    public String getPartner_verify_code() {
        return partner_verify_code;
    }

    public void setPartner_verify_code(String partner_verify_code) {
        this.partner_verify_code = partner_verify_code;
    }

    public String getRequest_time() {
        return request_time;
    }

    public void setRequest_time(String request_time) {
        this.request_time = request_time;
    }

    public String getSigned_msg() {
        return signed_msg;
    }

    public void setSigned_msg(String signed_msg) {
        this.signed_msg = signed_msg;
    }

    public List<PayTradeDetail> getTrade_details() {
        return trade_details;
    }

    public void setTrade_details(List<PayTradeDetail> trade_details) {
        this.trade_details = trade_details;
    }

    public TransSum getTrans_sum() {
        return trans_sum;
    }

    public void setTrans_sum(TransSum trans_sum) {
        this.trans_sum = trans_sum;
    }
}
